﻿using System;
using System.ComponentModel;
using System.Reactive.Subjects;
using System.Runtime.CompilerServices;

namespace Reactive.Bindings.Notifiers;

/// <summary>
/// Notify boolean flag.
/// </summary>
public class BooleanNotifierLegacy : IObservable<bool>, INotifyPropertyChanged
{
    /// <summary>
    /// Occurs when a property value changes.
    /// </summary>
    /// <returns></returns>
    public event PropertyChangedEventHandler? PropertyChanged;

    private readonly Subject<bool> boolTrigger = new();
    private bool boolValue;

    /// <summary>
    /// Current flag value
    /// </summary>
    public bool Value
    {
        get
        {
            return boolValue;
        }

        set
        {
            boolValue = value;
            OnPropertyChanged();
            boolTrigger.OnNext(value);
        }
    }

    /// <summary>
    /// Setup initial flag.
    /// </summary>
    public BooleanNotifierLegacy(bool initialValue = false)
    {
        Value = initialValue;
    }

    /// <summary>
    /// Set and raise true if current value isn't true.
    /// </summary>
    public void TurnOn()
    {
        if (Value != true)
        {
            Value = true;
        }
    }

    /// <summary>
    /// Set and raise false if current value isn't false.
    /// </summary>
    public void TurnOff()
    {
        if (Value != false)
        {
            Value = false;
        }
    }

    /// <summary>
    /// Set and raise reverse value.
    /// </summary>
    public void SwitchValue() => Value = !Value;

    /// <summary>
    /// Subscribe observer.
    /// </summary>
    public IDisposable Subscribe(IObserver<bool> observer) => boolTrigger.Subscribe(observer);

    /// <summary>
    /// Called when [property changed].
    /// </summary>
    /// <param name="propertyName">Name of the property.</param>
    protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
